import { createI18n } from 'vue-i18n'
import enUS from '../i18n/locales/en-US'
import zhCN from '../i18n/locales/zh-CN'

const messages = {
  'zh-CN': zhCN,
  'en-US': enUS
}

// 从localStorage获取保存的语言设置，默认为中文
const getStoredLanguage = () => {
  return localStorage.getItem('language') || 'zh-CN'
}

const i18n = createI18n({
  legacy: false, // 使用Composition API模式
  locale: getStoredLanguage(),
  fallbackLocale: 'zh-CN',
  messages,
  allowComposition: true,
  globalInjection: true
})

export default i18n

// 切换语言的工具函数
export const setLanguage = (lang: string) => {
  i18n.global.locale.value = lang as any
  localStorage.setItem('language', lang)
}

// 获取当前语言
export const getCurrentLanguage = () => {
  return i18n.global.locale.value
} 